<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>messageformat.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_messageformat.js.html">messageformat.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Message/plural format library with locale support.
<a name="line17"></a> *
<a name="line18"></a> * Message format grammar:
<a name="line19"></a> *
<a name="line20"></a> * messageFormatPattern := string ( &quot;{&quot; messageFormatElement &quot;}&quot; string )*
<a name="line21"></a> * messageFormatElement := argumentIndex [ &quot;,&quot; elementFormat ]
<a name="line22"></a> * elementFormat := &quot;plural&quot; &quot;,&quot; pluralStyle
<a name="line23"></a> *                  | &quot;selectordinal&quot; &quot;,&quot; ordinalStyle
<a name="line24"></a> *                  | &quot;select&quot; &quot;,&quot; selectStyle
<a name="line25"></a> * pluralStyle :=  pluralFormatPattern
<a name="line26"></a> * ordinalStyle :=  selectFormatPattern
<a name="line27"></a> * selectStyle :=  selectFormatPattern
<a name="line28"></a> * pluralFormatPattern := [ &quot;offset&quot; &quot;:&quot; offsetIndex ] pluralForms*
<a name="line29"></a> * selectFormatPattern := pluralForms*
<a name="line30"></a> * pluralForms := stringKey &quot;{&quot; ( &quot;{&quot; messageFormatElement &quot;}&quot;|string )* &quot;}&quot;
<a name="line31"></a> *
<a name="line32"></a> * This is a subset of the ICU MessageFormatSyntax:
<a name="line33"></a> *   http://userguide.icu-project.org/formatparse/messages
<a name="line34"></a> * See also http://go/plurals and http://go/ordinals for internal details.
<a name="line35"></a> *
<a name="line36"></a> *
<a name="line37"></a> * Message example:
<a name="line38"></a> *
<a name="line39"></a> * I see {NUM_PEOPLE, plural, offset:1
<a name="line40"></a> *         =0 {no one at all}
<a name="line41"></a> *         =1 {{WHO}}
<a name="line42"></a> *         one {{WHO} and one other person}
<a name="line43"></a> *         other {{WHO} and # other people}}
<a name="line44"></a> * in {PLACE}.
<a name="line45"></a> *
<a name="line46"></a> * Calling format({&#39;NUM_PEOPLE&#39;: 2, &#39;WHO&#39;: &#39;Mark&#39;, &#39;PLACE&#39;: &#39;Athens&#39;}) would
<a name="line47"></a> * produce &quot;I see Mark and one other person in Athens.&quot; as output.
<a name="line48"></a> *
<a name="line49"></a> * OR:
<a name="line50"></a> *
<a name="line51"></a> * {NUM_FLOOR, selectordinal,
<a name="line52"></a> *   one {Take the elevator to the #st floor.}
<a name="line53"></a> *   two {Take the elevator to the #nd floor.}
<a name="line54"></a> *   few {Take the elevator to the #rd floor.}
<a name="line55"></a> *   other {Take the elevator to the #th floor.}}
<a name="line56"></a> *
<a name="line57"></a> * Calling format({&#39;NUM_FLOOR&#39;: 22}) would produce
<a name="line58"></a> * &quot;Take the elevator to the 22nd floor&quot;.
<a name="line59"></a> *
<a name="line60"></a> * See messageformat_test.html for more examples.
<a name="line61"></a> */
<a name="line62"></a>
<a name="line63"></a>goog.provide(&#39;goog.i18n.MessageFormat&#39;);
<a name="line64"></a>
<a name="line65"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line66"></a>goog.require(&#39;goog.i18n.NumberFormat&#39;);
<a name="line67"></a>goog.require(&#39;goog.i18n.ordinalRules&#39;);
<a name="line68"></a>goog.require(&#39;goog.i18n.pluralRules&#39;);
<a name="line69"></a>
<a name="line70"></a>
<a name="line71"></a>
<a name="line72"></a>/**
<a name="line73"></a> * Constructor of MessageFormat.
<a name="line74"></a> * @param {string} pattern The pattern we parse and apply positional parameters
<a name="line75"></a> *     to.
<a name="line76"></a> * @constructor
<a name="line77"></a> */
<a name="line78"></a>goog.i18n.MessageFormat = function(pattern) {
<a name="line79"></a>  /**
<a name="line80"></a>   * All encountered literals during parse stage. Indices tell us the order of
<a name="line81"></a>   * replacement.
<a name="line82"></a>   * @type {!Array.&lt;string&gt;}
<a name="line83"></a>   * @private
<a name="line84"></a>   */
<a name="line85"></a>  this.literals_ = [];
<a name="line86"></a>
<a name="line87"></a>  /**
<a name="line88"></a>   * Input pattern gets parsed into objects for faster formatting.
<a name="line89"></a>   * @type {!Array.&lt;!Object&gt;}
<a name="line90"></a>   * @private
<a name="line91"></a>   */
<a name="line92"></a>  this.parsedPattern_ = [];
<a name="line93"></a>
<a name="line94"></a>  /**
<a name="line95"></a>   * Locale aware number formatter.
<a name="line96"></a>   * @type {goog.i18n.NumberFormat}
<a name="line97"></a>   * @private
<a name="line98"></a>   */
<a name="line99"></a>  this.numberFormatter_ = new goog.i18n.NumberFormat(
<a name="line100"></a>      goog.i18n.NumberFormat.Format.DECIMAL);
<a name="line101"></a>
<a name="line102"></a>  this.parsePattern_(pattern);
<a name="line103"></a>};
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Literal strings, including &#39;&#39;, are replaced with \uFDDF_x_ for
<a name="line108"></a> * parsing purposes, and recovered during format phase.
<a name="line109"></a> * \uFDDF is a Unicode nonprinting character, not expected to be found in the
<a name="line110"></a> * typical message.
<a name="line111"></a> * @type {string}
<a name="line112"></a> * @private
<a name="line113"></a> */
<a name="line114"></a>goog.i18n.MessageFormat.LITERAL_PLACEHOLDER_ = &#39;\uFDDF_&#39;;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * Marks a string and block during parsing.
<a name="line119"></a> * @enum {number}
<a name="line120"></a> * @private
<a name="line121"></a> */
<a name="line122"></a>goog.i18n.MessageFormat.Element_ = {
<a name="line123"></a>  STRING: 0,
<a name="line124"></a>  BLOCK: 1
<a name="line125"></a>};
<a name="line126"></a>
<a name="line127"></a>
<a name="line128"></a>/**
<a name="line129"></a> * Block type.
<a name="line130"></a> * @enum {number}
<a name="line131"></a> * @private
<a name="line132"></a> */
<a name="line133"></a>goog.i18n.MessageFormat.BlockType_ = {
<a name="line134"></a>  PLURAL: 0,
<a name="line135"></a>  ORDINAL: 1,
<a name="line136"></a>  SELECT: 2,
<a name="line137"></a>  SIMPLE: 3,
<a name="line138"></a>  STRING: 4,
<a name="line139"></a>  UNKNOWN: 5
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * Mandatory option in both select and plural form.
<a name="line145"></a> * @type {string}
<a name="line146"></a> * @private
<a name="line147"></a> */
<a name="line148"></a>goog.i18n.MessageFormat.OTHER_ = &#39;other&#39;;
<a name="line149"></a>
<a name="line150"></a>
<a name="line151"></a>/**
<a name="line152"></a> * Regular expression for looking for string literals.
<a name="line153"></a> * @type {RegExp}
<a name="line154"></a> * @private
<a name="line155"></a> */
<a name="line156"></a>goog.i18n.MessageFormat.REGEX_LITERAL_ = new RegExp(&quot;&#39;([{}#].*?)&#39;&quot;, &#39;g&#39;);
<a name="line157"></a>
<a name="line158"></a>
<a name="line159"></a>/**
<a name="line160"></a> * Regular expression for looking for &#39;&#39; in the message.
<a name="line161"></a> * @type {RegExp}
<a name="line162"></a> * @private
<a name="line163"></a> */
<a name="line164"></a>goog.i18n.MessageFormat.REGEX_DOUBLE_APOSTROPHE_ = new RegExp(&quot;&#39;&#39;&quot;, &#39;g&#39;);
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * Formats a message, treating &#39;#&#39; with special meaning representing
<a name="line169"></a> * the number (plural_variable - offset).
<a name="line170"></a> * @param {!Object} namedParameters Parameters that either
<a name="line171"></a> *     influence the formatting or are used as actual data.
<a name="line172"></a> *     I.e. in call to fmt.format({&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;}),
<a name="line173"></a> *     object {&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;} holds positional parameters.
<a name="line174"></a> *     1st parameter could mean 5 people, which could influence plural format,
<a name="line175"></a> *     and 2nd parameter is just a data to be printed out in proper position.
<a name="line176"></a> * @return {string} Formatted message.
<a name="line177"></a> */
<a name="line178"></a>goog.i18n.MessageFormat.prototype.format = function(namedParameters) {
<a name="line179"></a>  return this.format_(namedParameters, false);
<a name="line180"></a>};
<a name="line181"></a>
<a name="line182"></a>
<a name="line183"></a>/**
<a name="line184"></a> * Formats a message, treating &#39;#&#39; as literary character.
<a name="line185"></a> * @param {!Object} namedParameters Parameters that either
<a name="line186"></a> *     influence the formatting or are used as actual data.
<a name="line187"></a> *     I.e. in call to fmt.format({&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;}),
<a name="line188"></a> *     object {&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;} holds positional parameters.
<a name="line189"></a> *     1st parameter could mean 5 people, which could influence plural format,
<a name="line190"></a> *     and 2nd parameter is just a data to be printed out in proper position.
<a name="line191"></a> * @return {string} Formatted message.
<a name="line192"></a> */
<a name="line193"></a>goog.i18n.MessageFormat.prototype.formatIgnoringPound =
<a name="line194"></a>    function(namedParameters) {
<a name="line195"></a>  return this.format_(namedParameters, true);
<a name="line196"></a>};
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>/**
<a name="line200"></a> * Formats a message.
<a name="line201"></a> * @param {!Object} namedParameters Parameters that either
<a name="line202"></a> *     influence the formatting or are used as actual data.
<a name="line203"></a> *     I.e. in call to fmt.format({&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;}),
<a name="line204"></a> *     object {&#39;NUM_PEOPLE&#39;: 5, &#39;NAME&#39;: &#39;Angela&#39;} holds positional parameters.
<a name="line205"></a> *     1st parameter could mean 5 people, which could influence plural format,
<a name="line206"></a> *     and 2nd parameter is just a data to be printed out in proper position.
<a name="line207"></a> * @param {boolean} ignorePound If true, treat &#39;#&#39; in plural messages as a
<a name="line208"></a> *     literary character, else treat it as an ICU syntax character, resolving
<a name="line209"></a> *     to the number (plural_variable - offset).
<a name="line210"></a> * @return {string} Formatted message.
<a name="line211"></a> * @private
<a name="line212"></a> */
<a name="line213"></a>goog.i18n.MessageFormat.prototype.format_ =
<a name="line214"></a>    function(namedParameters, ignorePound) {
<a name="line215"></a>  if (this.parsedPattern_.length == 0) {
<a name="line216"></a>    return &#39;&#39;;
<a name="line217"></a>  }
<a name="line218"></a>
<a name="line219"></a>  var result = [];
<a name="line220"></a>  this.formatBlock_(this.parsedPattern_, namedParameters, ignorePound, result);
<a name="line221"></a>  var message = result.join(&#39;&#39;);
<a name="line222"></a>
<a name="line223"></a>  if (!ignorePound) {
<a name="line224"></a>    goog.asserts.assert(message.search(&#39;#&#39;) == -1, &#39;Not all # were replaced.&#39;);
<a name="line225"></a>  }
<a name="line226"></a>
<a name="line227"></a>  while (this.literals_.length &gt; 0) {
<a name="line228"></a>    message = message.replace(this.buildPlaceholder_(this.literals_),
<a name="line229"></a>                              this.literals_.pop());
<a name="line230"></a>  }
<a name="line231"></a>
<a name="line232"></a>  return message;
<a name="line233"></a>};
<a name="line234"></a>
<a name="line235"></a>
<a name="line236"></a>/**
<a name="line237"></a> * Parses generic block and returns a formatted string.
<a name="line238"></a> * @param {!Array.&lt;!Object&gt;} parsedPattern Holds parsed tree.
<a name="line239"></a> * @param {!Object} namedParameters Parameters that either influence
<a name="line240"></a> *     the formatting or are used as actual data.
<a name="line241"></a> * @param {boolean} ignorePound If true, treat &#39;#&#39; in plural messages as a
<a name="line242"></a> *     literary character, else treat it as an ICU syntax character, resolving
<a name="line243"></a> *     to the number (plural_variable - offset).
<a name="line244"></a> * @param {!Array.&lt;!string&gt;} result Each formatting stage appends its product
<a name="line245"></a> *     to the result.
<a name="line246"></a> * @private
<a name="line247"></a> */
<a name="line248"></a>goog.i18n.MessageFormat.prototype.formatBlock_ = function(
<a name="line249"></a>    parsedPattern, namedParameters, ignorePound, result) {
<a name="line250"></a>  for (var i = 0; i &lt; parsedPattern.length; i++) {
<a name="line251"></a>    switch (parsedPattern[i].type) {
<a name="line252"></a>      case goog.i18n.MessageFormat.BlockType_.STRING:
<a name="line253"></a>        result.push(parsedPattern[i].value);
<a name="line254"></a>        break;
<a name="line255"></a>      case goog.i18n.MessageFormat.BlockType_.SIMPLE:
<a name="line256"></a>        var pattern = parsedPattern[i].value;
<a name="line257"></a>        this.formatSimplePlaceholder_(pattern, namedParameters, result);
<a name="line258"></a>        break;
<a name="line259"></a>      case goog.i18n.MessageFormat.BlockType_.SELECT:
<a name="line260"></a>        var pattern = parsedPattern[i].value;
<a name="line261"></a>        this.formatSelectBlock_(pattern, namedParameters, ignorePound, result);
<a name="line262"></a>        break;
<a name="line263"></a>      case goog.i18n.MessageFormat.BlockType_.PLURAL:
<a name="line264"></a>        var pattern = parsedPattern[i].value;
<a name="line265"></a>        this.formatPluralOrdinalBlock_(pattern,
<a name="line266"></a>                                       namedParameters,
<a name="line267"></a>                                       goog.i18n.pluralRules.select,
<a name="line268"></a>                                       ignorePound,
<a name="line269"></a>                                       result);
<a name="line270"></a>        break;
<a name="line271"></a>      case goog.i18n.MessageFormat.BlockType_.ORDINAL:
<a name="line272"></a>        var pattern = parsedPattern[i].value;
<a name="line273"></a>        this.formatPluralOrdinalBlock_(pattern,
<a name="line274"></a>                                       namedParameters,
<a name="line275"></a>                                       goog.i18n.ordinalRules.select,
<a name="line276"></a>                                       ignorePound,
<a name="line277"></a>                                       result);
<a name="line278"></a>        break;
<a name="line279"></a>      default:
<a name="line280"></a>        goog.asserts.fail(&#39;Unrecognized block type.&#39;);
<a name="line281"></a>    }
<a name="line282"></a>  }
<a name="line283"></a>};
<a name="line284"></a>
<a name="line285"></a>
<a name="line286"></a>/**
<a name="line287"></a> * Formats simple placeholder.
<a name="line288"></a> * @param {!Object} parsedPattern JSON object containing placeholder info.
<a name="line289"></a> * @param {!Object} namedParameters Parameters that are used as actual data.
<a name="line290"></a> * @param {!Array.&lt;!string&gt;} result Each formatting stage appends its product
<a name="line291"></a> *     to the result.
<a name="line292"></a> * @private
<a name="line293"></a> */
<a name="line294"></a>goog.i18n.MessageFormat.prototype.formatSimplePlaceholder_ = function(
<a name="line295"></a>    parsedPattern, namedParameters, result) {
<a name="line296"></a>  var value = namedParameters[parsedPattern];
<a name="line297"></a>  if (!goog.isDef(value)) {
<a name="line298"></a>    result.push(&#39;Undefined parameter - &#39; + parsedPattern);
<a name="line299"></a>    return;
<a name="line300"></a>  }
<a name="line301"></a>
<a name="line302"></a>  // Don&#39;t push the value yet, it may contain any of # { } in it which
<a name="line303"></a>  // will break formatter. Insert a placeholder and replace at the end.
<a name="line304"></a>  this.literals_.push(value);
<a name="line305"></a>  result.push(this.buildPlaceholder_(this.literals_));
<a name="line306"></a>};
<a name="line307"></a>
<a name="line308"></a>
<a name="line309"></a>/**
<a name="line310"></a> * Formats select block. Only one option is selected.
<a name="line311"></a> * @param {!Object} parsedPattern JSON object containing select block info.
<a name="line312"></a> * @param {!Object} namedParameters Parameters that either influence
<a name="line313"></a> *     the formatting or are used as actual data.
<a name="line314"></a> * @param {boolean} ignorePound If true, treat &#39;#&#39; in plural messages as a
<a name="line315"></a> *     literary character, else treat it as an ICU syntax character, resolving
<a name="line316"></a> *     to the number (plural_variable - offset).
<a name="line317"></a> * @param {!Array.&lt;!string&gt;} result Each formatting stage appends its product
<a name="line318"></a> *     to the result.
<a name="line319"></a> * @private
<a name="line320"></a> */
<a name="line321"></a>goog.i18n.MessageFormat.prototype.formatSelectBlock_ = function(
<a name="line322"></a>    parsedPattern, namedParameters, ignorePound, result) {
<a name="line323"></a>  var argumentIndex = parsedPattern.argumentIndex;
<a name="line324"></a>  if (!goog.isDef(namedParameters[argumentIndex])) {
<a name="line325"></a>    result.push(&#39;Undefined parameter - &#39; + argumentIndex);
<a name="line326"></a>    return;
<a name="line327"></a>  }
<a name="line328"></a>
<a name="line329"></a>  var option = parsedPattern[namedParameters[argumentIndex]];
<a name="line330"></a>  if (!goog.isDef(option)) {
<a name="line331"></a>    option = parsedPattern[goog.i18n.MessageFormat.OTHER_];
<a name="line332"></a>    goog.asserts.assertArray(
<a name="line333"></a>        option, &#39;Invalid option or missing other option for select block.&#39;);
<a name="line334"></a>  }
<a name="line335"></a>
<a name="line336"></a>  this.formatBlock_(option, namedParameters, ignorePound, result);
<a name="line337"></a>};
<a name="line338"></a>
<a name="line339"></a>
<a name="line340"></a>/**
<a name="line341"></a> * Formats plural or selectordinal block. Only one option is selected and all #
<a name="line342"></a> * are replaced.
<a name="line343"></a> * @param {!Object} parsedPattern JSON object containing plural block info.
<a name="line344"></a> * @param {!Object} namedParameters Parameters that either influence
<a name="line345"></a> *     the formatting or are used as actual data.
<a name="line346"></a> * @param {!function(number):string} pluralSelector  A select function from
<a name="line347"></a> *     goog.i18n.pluralRules or goog.i18n.ordinalRules which determines which
<a name="line348"></a> *     plural/ordinal form to use based on the input number&#39;s cardinality.
<a name="line349"></a> * @param {boolean} ignorePound If true, treat &#39;#&#39; in plural messages as a
<a name="line350"></a> *     literary character, else treat it as an ICU syntax character, resolving
<a name="line351"></a> *     to the number (plural_variable - offset).
<a name="line352"></a> * @param {!Array.&lt;!string&gt;} result Each formatting stage appends its product
<a name="line353"></a> *     to the result.
<a name="line354"></a> * @private
<a name="line355"></a> */
<a name="line356"></a>goog.i18n.MessageFormat.prototype.formatPluralOrdinalBlock_ = function(
<a name="line357"></a>    parsedPattern, namedParameters, pluralSelector, ignorePound, result) {
<a name="line358"></a>  var argumentIndex = parsedPattern.argumentIndex;
<a name="line359"></a>  var argumentOffset = parsedPattern.argumentOffset;
<a name="line360"></a>  var pluralValue = +namedParameters[argumentIndex];
<a name="line361"></a>  if (isNaN(pluralValue)) {
<a name="line362"></a>    // TODO(user): Distinguish between undefined and invalid parameters.
<a name="line363"></a>    result.push(&#39;Undefined or invalid parameter - &#39; + argumentIndex);
<a name="line364"></a>    return;
<a name="line365"></a>  }
<a name="line366"></a>  var diff = pluralValue - argumentOffset;
<a name="line367"></a>
<a name="line368"></a>  // Check if there is an exact match.
<a name="line369"></a>  var option = parsedPattern[namedParameters[argumentIndex]];
<a name="line370"></a>  if (!goog.isDef(option)) {
<a name="line371"></a>    goog.asserts.assert(diff &gt;= 0, &#39;Argument index smaller than offset.&#39;);
<a name="line372"></a>
<a name="line373"></a>    var item = pluralSelector(diff);
<a name="line374"></a>    goog.asserts.assertString(item, &#39;Invalid plural key.&#39;);
<a name="line375"></a>
<a name="line376"></a>    option = parsedPattern[item];
<a name="line377"></a>
<a name="line378"></a>    // If option is not provided fall back to &quot;other&quot;.
<a name="line379"></a>    if (!goog.isDef(option)) {
<a name="line380"></a>      option = parsedPattern[goog.i18n.MessageFormat.OTHER_];
<a name="line381"></a>    }
<a name="line382"></a>
<a name="line383"></a>    goog.asserts.assertArray(
<a name="line384"></a>        option, &#39;Invalid option or missing other option for plural block.&#39;);
<a name="line385"></a>  }
<a name="line386"></a>
<a name="line387"></a>  var pluralResult = [];
<a name="line388"></a>  this.formatBlock_(option, namedParameters, ignorePound, pluralResult);
<a name="line389"></a>  var plural = pluralResult.join(&#39;&#39;);
<a name="line390"></a>  goog.asserts.assertString(plural, &#39;Empty block in plural.&#39;);
<a name="line391"></a>  if (ignorePound) {
<a name="line392"></a>    result.push(plural);
<a name="line393"></a>  } else {
<a name="line394"></a>    var localeAwareDiff = this.numberFormatter_.format(diff);
<a name="line395"></a>    result.push(plural.replace(/#/g, localeAwareDiff));
<a name="line396"></a>  }
<a name="line397"></a>};
<a name="line398"></a>
<a name="line399"></a>
<a name="line400"></a>/**
<a name="line401"></a> * Parses input pattern into an array, for faster reformatting with
<a name="line402"></a> * different input parameters.
<a name="line403"></a> * Parsing is locale independent.
<a name="line404"></a> * @param {string} pattern MessageFormat pattern to parse.
<a name="line405"></a> * @private
<a name="line406"></a> */
<a name="line407"></a>goog.i18n.MessageFormat.prototype.parsePattern_ = function(pattern) {
<a name="line408"></a>  if (pattern) {
<a name="line409"></a>    pattern = this.insertPlaceholders_(pattern);
<a name="line410"></a>
<a name="line411"></a>    this.parsedPattern_ = this.parseBlock_(pattern);
<a name="line412"></a>  }
<a name="line413"></a>};
<a name="line414"></a>
<a name="line415"></a>
<a name="line416"></a>/**
<a name="line417"></a> * Replaces string literals with literal placeholders.
<a name="line418"></a> * Literals are string of the form &#39;}...&#39;, &#39;{...&#39; and &#39;#...&#39; where ... is
<a name="line419"></a> * set of characters not containing &#39;
<a name="line420"></a> * Builds a dictionary so we can recover literals during format phase.
<a name="line421"></a> * @param {string} pattern Pattern to clean up.
<a name="line422"></a> * @return {string} Pattern with literals replaced with placeholders.
<a name="line423"></a> * @private
<a name="line424"></a> */
<a name="line425"></a>goog.i18n.MessageFormat.prototype.insertPlaceholders_ = function(pattern) {
<a name="line426"></a>  var literals = this.literals_;
<a name="line427"></a>  var buildPlaceholder = goog.bind(this.buildPlaceholder_, this);
<a name="line428"></a>
<a name="line429"></a>  // First replace &#39;&#39; with single quote placeholder since they can be found
<a name="line430"></a>  // inside other literals.
<a name="line431"></a>  pattern = pattern.replace(
<a name="line432"></a>      goog.i18n.MessageFormat.REGEX_DOUBLE_APOSTROPHE_,
<a name="line433"></a>      function() {
<a name="line434"></a>        literals.push(&quot;&#39;&quot;);
<a name="line435"></a>        return buildPlaceholder(literals);
<a name="line436"></a>      });
<a name="line437"></a>
<a name="line438"></a>  pattern = pattern.replace(
<a name="line439"></a>      goog.i18n.MessageFormat.REGEX_LITERAL_,
<a name="line440"></a>      function(match, text) {
<a name="line441"></a>        literals.push(text);
<a name="line442"></a>        return buildPlaceholder(literals);
<a name="line443"></a>      });
<a name="line444"></a>
<a name="line445"></a>  return pattern;
<a name="line446"></a>};
<a name="line447"></a>
<a name="line448"></a>
<a name="line449"></a>/**
<a name="line450"></a> * Breaks pattern into strings and top level {...} blocks.
<a name="line451"></a> * @param {string} pattern (sub)Pattern to be broken.
<a name="line452"></a> * @return {Array.&lt;Object&gt;} Each item is {type, value}.
<a name="line453"></a> * @private
<a name="line454"></a> */
<a name="line455"></a>goog.i18n.MessageFormat.prototype.extractParts_ = function(pattern) {
<a name="line456"></a>  var prevPos = 0;
<a name="line457"></a>  var inBlock = false;
<a name="line458"></a>  var braceStack = [];
<a name="line459"></a>  var results = [];
<a name="line460"></a>
<a name="line461"></a>  var braces = /[{}]/g;
<a name="line462"></a>  braces.lastIndex = 0;  // lastIndex doesn&#39;t get set to 0 so we have to.
<a name="line463"></a>  var match;
<a name="line464"></a>
<a name="line465"></a>  while (match = braces.exec(pattern)) {
<a name="line466"></a>    var pos = match.index;
<a name="line467"></a>    if (match[0] == &#39;}&#39;) {
<a name="line468"></a>      var brace = braceStack.pop();
<a name="line469"></a>      goog.asserts.assert(goog.isDef(brace) &amp;&amp; brace == &#39;{&#39;,
<a name="line470"></a>                          &#39;No matching { for }.&#39;);
<a name="line471"></a>
<a name="line472"></a>      if (braceStack.length == 0) {
<a name="line473"></a>        // End of the block.
<a name="line474"></a>        var part = {};
<a name="line475"></a>        part.type = goog.i18n.MessageFormat.Element_.BLOCK;
<a name="line476"></a>        part.value = pattern.substring(prevPos, pos);
<a name="line477"></a>        results.push(part);
<a name="line478"></a>        prevPos = pos + 1;
<a name="line479"></a>        inBlock = false;
<a name="line480"></a>      }
<a name="line481"></a>    } else {
<a name="line482"></a>      if (braceStack.length == 0) {
<a name="line483"></a>        inBlock = true;
<a name="line484"></a>        var substring = pattern.substring(prevPos, pos);
<a name="line485"></a>        if (substring != &#39;&#39;) {
<a name="line486"></a>          results.push({
<a name="line487"></a>            type: goog.i18n.MessageFormat.Element_.STRING,
<a name="line488"></a>            value: substring
<a name="line489"></a>          });
<a name="line490"></a>        }
<a name="line491"></a>        prevPos = pos + 1;
<a name="line492"></a>      }
<a name="line493"></a>      braceStack.push(&#39;{&#39;);
<a name="line494"></a>    }
<a name="line495"></a>  }
<a name="line496"></a>
<a name="line497"></a>  // Take care of the final string, and check if the braceStack is empty.
<a name="line498"></a>  goog.asserts.assert(braceStack.length == 0,
<a name="line499"></a>                      &#39;There are mismatched { or } in the pattern.&#39;);
<a name="line500"></a>
<a name="line501"></a>  var substring = pattern.substring(prevPos);
<a name="line502"></a>  if (substring != &#39;&#39;) {
<a name="line503"></a>    results.push({
<a name="line504"></a>      type: goog.i18n.MessageFormat.Element_.STRING,
<a name="line505"></a>      value: substring
<a name="line506"></a>    });
<a name="line507"></a>  }
<a name="line508"></a>
<a name="line509"></a>  return results;
<a name="line510"></a>};
<a name="line511"></a>
<a name="line512"></a>
<a name="line513"></a>/**
<a name="line514"></a> * A regular expression to parse the plural block, extracting the argument
<a name="line515"></a> * index and offset (if any).
<a name="line516"></a> * @type {RegExp}
<a name="line517"></a> * @private
<a name="line518"></a> */
<a name="line519"></a>goog.i18n.MessageFormat.PLURAL_BLOCK_RE_ =
<a name="line520"></a>    /^\s*(\w+)\s*,\s*plural\s*,(?:\s*offset:(\d+))?/;
<a name="line521"></a>
<a name="line522"></a>
<a name="line523"></a>/**
<a name="line524"></a> * A regular expression to parse the ordinal block, extracting the argument
<a name="line525"></a> * index.
<a name="line526"></a> * @type {RegExp}
<a name="line527"></a> * @private
<a name="line528"></a> */
<a name="line529"></a>goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_ = /^\s*(\w+)\s*,\s*selectordinal\s*,/;
<a name="line530"></a>
<a name="line531"></a>
<a name="line532"></a>/**
<a name="line533"></a> * A regular expression to parse the select block, extracting the argument
<a name="line534"></a> * index.
<a name="line535"></a> * @type {RegExp}
<a name="line536"></a> * @private
<a name="line537"></a> */
<a name="line538"></a>goog.i18n.MessageFormat.SELECT_BLOCK_RE_ = /^\s*(\w+)\s*,\s*select\s*,/;
<a name="line539"></a>
<a name="line540"></a>
<a name="line541"></a>/**
<a name="line542"></a> * Detects which type of a block is the pattern.
<a name="line543"></a> * @param {string} pattern Content of the block.
<a name="line544"></a> * @return {goog.i18n.MessageFormat.BlockType_} One of the block types.
<a name="line545"></a> * @private
<a name="line546"></a> */
<a name="line547"></a>goog.i18n.MessageFormat.prototype.parseBlockType_ = function(pattern) {
<a name="line548"></a>  if (goog.i18n.MessageFormat.PLURAL_BLOCK_RE_.test(pattern)) {
<a name="line549"></a>    return goog.i18n.MessageFormat.BlockType_.PLURAL;
<a name="line550"></a>  }
<a name="line551"></a>
<a name="line552"></a>  if (goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_.test(pattern)) {
<a name="line553"></a>    return goog.i18n.MessageFormat.BlockType_.ORDINAL;
<a name="line554"></a>  }
<a name="line555"></a>
<a name="line556"></a>  if (goog.i18n.MessageFormat.SELECT_BLOCK_RE_.test(pattern)) {
<a name="line557"></a>    return goog.i18n.MessageFormat.BlockType_.SELECT;
<a name="line558"></a>  }
<a name="line559"></a>
<a name="line560"></a>  if (/^\s*\w+\s*/.test(pattern)) {
<a name="line561"></a>    return goog.i18n.MessageFormat.BlockType_.SIMPLE;
<a name="line562"></a>  }
<a name="line563"></a>
<a name="line564"></a>  return goog.i18n.MessageFormat.BlockType_.UNKNOWN;
<a name="line565"></a>};
<a name="line566"></a>
<a name="line567"></a>
<a name="line568"></a>/**
<a name="line569"></a> * Parses generic block.
<a name="line570"></a> * @param {string} pattern Content of the block to parse.
<a name="line571"></a> * @return {!Array.&lt;!Object&gt;} Subblocks marked as strings, select...
<a name="line572"></a> * @private
<a name="line573"></a> */
<a name="line574"></a>goog.i18n.MessageFormat.prototype.parseBlock_ = function(pattern) {
<a name="line575"></a>  var result = [];
<a name="line576"></a>  var parts = this.extractParts_(pattern);
<a name="line577"></a>  for (var i = 0; i &lt; parts.length; i++) {
<a name="line578"></a>    var block = {};
<a name="line579"></a>    if (goog.i18n.MessageFormat.Element_.STRING == parts[i].type) {
<a name="line580"></a>      block.type = goog.i18n.MessageFormat.BlockType_.STRING;
<a name="line581"></a>      block.value = parts[i].value;
<a name="line582"></a>    } else if (goog.i18n.MessageFormat.Element_.BLOCK == parts[i].type) {
<a name="line583"></a>      var blockType = this.parseBlockType_(parts[i].value);
<a name="line584"></a>
<a name="line585"></a>      switch (blockType) {
<a name="line586"></a>        case goog.i18n.MessageFormat.BlockType_.SELECT:
<a name="line587"></a>          block.type = goog.i18n.MessageFormat.BlockType_.SELECT;
<a name="line588"></a>          block.value = this.parseSelectBlock_(parts[i].value);
<a name="line589"></a>          break;
<a name="line590"></a>        case goog.i18n.MessageFormat.BlockType_.PLURAL:
<a name="line591"></a>          block.type = goog.i18n.MessageFormat.BlockType_.PLURAL;
<a name="line592"></a>          block.value = this.parsePluralBlock_(parts[i].value);
<a name="line593"></a>          break;
<a name="line594"></a>        case goog.i18n.MessageFormat.BlockType_.ORDINAL:
<a name="line595"></a>          block.type = goog.i18n.MessageFormat.BlockType_.ORDINAL;
<a name="line596"></a>          block.value = this.parseOrdinalBlock_(parts[i].value);
<a name="line597"></a>          break;
<a name="line598"></a>        case goog.i18n.MessageFormat.BlockType_.SIMPLE:
<a name="line599"></a>          block.type = goog.i18n.MessageFormat.BlockType_.SIMPLE;
<a name="line600"></a>          block.value = parts[i].value;
<a name="line601"></a>          break;
<a name="line602"></a>        default:
<a name="line603"></a>          goog.asserts.fail(&#39;Unknown block type.&#39;);
<a name="line604"></a>      }
<a name="line605"></a>    } else {
<a name="line606"></a>      goog.asserts.fail(&#39;Unknown part of the pattern.&#39;);
<a name="line607"></a>    }
<a name="line608"></a>    result.push(block);
<a name="line609"></a>  }
<a name="line610"></a>
<a name="line611"></a>  return result;
<a name="line612"></a>};
<a name="line613"></a>
<a name="line614"></a>
<a name="line615"></a>/**
<a name="line616"></a> * Parses a select type of a block and produces JSON object for it.
<a name="line617"></a> * @param {string} pattern Subpattern that needs to be parsed as select pattern.
<a name="line618"></a> * @return {Object} Object with select block info.
<a name="line619"></a> * @private
<a name="line620"></a> */
<a name="line621"></a>goog.i18n.MessageFormat.prototype.parseSelectBlock_ = function(pattern) {
<a name="line622"></a>  var argumentIndex = &#39;&#39;;
<a name="line623"></a>  var replaceRegex = goog.i18n.MessageFormat.SELECT_BLOCK_RE_;
<a name="line624"></a>  pattern = pattern.replace(replaceRegex, function(string, name) {
<a name="line625"></a>    argumentIndex = name;
<a name="line626"></a>    return &#39;&#39;;
<a name="line627"></a>  });
<a name="line628"></a>  var result = {};
<a name="line629"></a>  result.argumentIndex = argumentIndex;
<a name="line630"></a>
<a name="line631"></a>  var parts = this.extractParts_(pattern);
<a name="line632"></a>  // Looking for (key block)+ sequence. One of the keys has to be &quot;other&quot;.
<a name="line633"></a>  var pos = 0;
<a name="line634"></a>  while (pos &lt; parts.length) {
<a name="line635"></a>    var key = parts[pos].value;
<a name="line636"></a>    goog.asserts.assertString(key, &#39;Missing select key element.&#39;);
<a name="line637"></a>
<a name="line638"></a>    pos++;
<a name="line639"></a>    goog.asserts.assert(pos &lt; parts.length,
<a name="line640"></a>                        &#39;Missing or invalid select value element.&#39;);
<a name="line641"></a>
<a name="line642"></a>    if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
<a name="line643"></a>      var value = this.parseBlock_(parts[pos].value);
<a name="line644"></a>    } else {
<a name="line645"></a>      goog.asserts.fail(&#39;Expected block type.&#39;);
<a name="line646"></a>    }
<a name="line647"></a>    result[key.replace(/\s/g, &#39;&#39;)] = value;
<a name="line648"></a>    pos++;
<a name="line649"></a>  }
<a name="line650"></a>
<a name="line651"></a>  goog.asserts.assertArray(result[goog.i18n.MessageFormat.OTHER_],
<a name="line652"></a>                           &#39;Missing other key in select statement.&#39;);
<a name="line653"></a>  return result;
<a name="line654"></a>};
<a name="line655"></a>
<a name="line656"></a>
<a name="line657"></a>/**
<a name="line658"></a> * Parses a plural type of a block and produces JSON object for it.
<a name="line659"></a> * @param {string} pattern Subpattern that needs to be parsed as plural pattern.
<a name="line660"></a> * @return {Object} Object with select block info.
<a name="line661"></a> * @private
<a name="line662"></a> */
<a name="line663"></a>goog.i18n.MessageFormat.prototype.parsePluralBlock_ = function(pattern) {
<a name="line664"></a>  var argumentIndex = &#39;&#39;;
<a name="line665"></a>  var argumentOffset = 0;
<a name="line666"></a>  var replaceRegex = goog.i18n.MessageFormat.PLURAL_BLOCK_RE_;
<a name="line667"></a>  pattern = pattern.replace(replaceRegex, function(string, name, offset) {
<a name="line668"></a>    argumentIndex = name;
<a name="line669"></a>    if (offset) {
<a name="line670"></a>      argumentOffset = parseInt(offset, 10);
<a name="line671"></a>    }
<a name="line672"></a>    return &#39;&#39;;
<a name="line673"></a>  });
<a name="line674"></a>
<a name="line675"></a>  var result = {};
<a name="line676"></a>  result.argumentIndex = argumentIndex;
<a name="line677"></a>  result.argumentOffset = argumentOffset;
<a name="line678"></a>
<a name="line679"></a>  var parts = this.extractParts_(pattern);
<a name="line680"></a>  // Looking for (key block)+ sequence.
<a name="line681"></a>  var pos = 0;
<a name="line682"></a>  while (pos &lt; parts.length) {
<a name="line683"></a>    var key = parts[pos].value;
<a name="line684"></a>    goog.asserts.assertString(key, &#39;Missing plural key element.&#39;);
<a name="line685"></a>
<a name="line686"></a>    pos++;
<a name="line687"></a>    goog.asserts.assert(pos &lt; parts.length,
<a name="line688"></a>                        &#39;Missing or invalid plural value element.&#39;);
<a name="line689"></a>
<a name="line690"></a>    if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
<a name="line691"></a>      var value = this.parseBlock_(parts[pos].value);
<a name="line692"></a>    } else {
<a name="line693"></a>      goog.asserts.fail(&#39;Expected block type.&#39;);
<a name="line694"></a>    }
<a name="line695"></a>    result[key.replace(/\s*(?:=)?(\w+)\s*/, &#39;$1&#39;)] = value;
<a name="line696"></a>    pos++;
<a name="line697"></a>  }
<a name="line698"></a>
<a name="line699"></a>  goog.asserts.assertArray(result[goog.i18n.MessageFormat.OTHER_],
<a name="line700"></a>                           &#39;Missing other key in plural statement.&#39;);
<a name="line701"></a>
<a name="line702"></a>  return result;
<a name="line703"></a>};
<a name="line704"></a>
<a name="line705"></a>
<a name="line706"></a>/**
<a name="line707"></a> * Parses an ordinal type of a block and produces JSON object for it.
<a name="line708"></a> * For example the input string:
<a name="line709"></a> *  &#39;{FOO, selectordinal, one {Message A}other {Message B}}&#39;
<a name="line710"></a> * Should result in the output object:
<a name="line711"></a> * {
<a name="line712"></a> *   argumentIndex: &#39;FOO&#39;,
<a name="line713"></a> *   argumentOffest: 0,
<a name="line714"></a> *   one: [ { type: 4, value: &#39;Message A&#39; } ],
<a name="line715"></a> *   other: [ { type: 4, value: &#39;Message B&#39; } ]
<a name="line716"></a> * }
<a name="line717"></a> * @param {string} pattern Subpattern that needs to be parsed as plural pattern.
<a name="line718"></a> * @return {Object} Object with select block info.
<a name="line719"></a> * @private
<a name="line720"></a> */
<a name="line721"></a>goog.i18n.MessageFormat.prototype.parseOrdinalBlock_ = function(pattern) {
<a name="line722"></a>  var argumentIndex = &#39;&#39;;
<a name="line723"></a>  var replaceRegex = goog.i18n.MessageFormat.ORDINAL_BLOCK_RE_;
<a name="line724"></a>  pattern = pattern.replace(replaceRegex, function(string, name) {
<a name="line725"></a>    argumentIndex = name;
<a name="line726"></a>    return &#39;&#39;;
<a name="line727"></a>  });
<a name="line728"></a>
<a name="line729"></a>  var result = {};
<a name="line730"></a>  result.argumentIndex = argumentIndex;
<a name="line731"></a>  result.argumentOffset = 0;
<a name="line732"></a>
<a name="line733"></a>  var parts = this.extractParts_(pattern);
<a name="line734"></a>  // Looking for (key block)+ sequence.
<a name="line735"></a>  var pos = 0;
<a name="line736"></a>  while (pos &lt; parts.length) {
<a name="line737"></a>    var key = parts[pos].value;
<a name="line738"></a>    goog.asserts.assertString(key, &#39;Missing ordinal key element.&#39;);
<a name="line739"></a>
<a name="line740"></a>    pos++;
<a name="line741"></a>    goog.asserts.assert(pos &lt; parts.length,
<a name="line742"></a>                        &#39;Missing or invalid ordinal value element.&#39;);
<a name="line743"></a>
<a name="line744"></a>    if (goog.i18n.MessageFormat.Element_.BLOCK == parts[pos].type) {
<a name="line745"></a>      var value = this.parseBlock_(parts[pos].value);
<a name="line746"></a>    } else {
<a name="line747"></a>      goog.asserts.fail(&#39;Expected block type.&#39;);
<a name="line748"></a>    }
<a name="line749"></a>    result[key.replace(/\s*(?:=)?(\w+)\s*/, &#39;$1&#39;)] = value;
<a name="line750"></a>    pos++;
<a name="line751"></a>  }
<a name="line752"></a>
<a name="line753"></a>  goog.asserts.assertArray(result[goog.i18n.MessageFormat.OTHER_],
<a name="line754"></a>                           &#39;Missing other key in selectordinal statement.&#39;);
<a name="line755"></a>
<a name="line756"></a>  return result;
<a name="line757"></a>};
<a name="line758"></a>
<a name="line759"></a>
<a name="line760"></a>/**
<a name="line761"></a> * Builds a placeholder from the last index of the array.
<a name="line762"></a> * @param {!Array} literals All literals encountered during parse.
<a name="line763"></a> * @return {string} \uFDDF_ + last index + _.
<a name="line764"></a> * @private
<a name="line765"></a> */
<a name="line766"></a>goog.i18n.MessageFormat.prototype.buildPlaceholder_ = function(literals) {
<a name="line767"></a>  goog.asserts.assert(literals.length &gt; 0, &#39;Literal array is empty.&#39;);
<a name="line768"></a>
<a name="line769"></a>  var index = (literals.length - 1).toString(10);
<a name="line770"></a>  return goog.i18n.MessageFormat.LITERAL_PLACEHOLDER_ + index + &#39;_&#39;;
<a name="line771"></a>};
</pre>


</body>
</html>
